<?php

class C_signup extends CI_Controller {

	public function index(){
		//Loading page header.
		$this->load->view('templates/v_header');
		
		//Determine if the user is loged in.
		if ($this->session->userdata('user_loged_in')) {
			//If the user is loged in then redirect to start page.
			redirect('c_start');
		}
		else {
			//Load basic meny (for guests).
			$this->load->view('templates/v_menu_basic');
			
			//Loading validation library.
			$this->load->library('form_validation');
			
			//Setting the delimiters for error messages so they can be styled by CSS.
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
			
			//Setting validation rules.
			$this->form_validation->set_rules('username', 'username', 'required|is_unique[users.username]');
			$this->form_validation->set_rules('password1', 'password', 'required|min_length[3]|matches[password2]');
			$this->form_validation->set_rules('email1', 'e-mail', 'required|valid_email|matches[email2]');
			$this->form_validation->set_rules('birthday', 'birthday', 'required|callback__birtday_check');
			$this->form_validation->set_rules('gender', 'gender', 'required|callback__gender_check');
			$this->form_validation->set_rules('agreement', 'terms of use', 'callback__agreement_check');
			
			//Customising the error messages.
			$this->form_validation->set_message('is_unique', 'The selected %s is already taken.');
			$this->form_validation->set_message('required', 'You have to enter your %s.');
			$this->form_validation->set_message('matches', "The %s doesn't match the retype field.");
			
			//Determine if the entered data was OK.
			if ($this->form_validation->run() == FALSE) {
				//Load form helper.
				$this->load->helper('form');
				
				//Repopulating the form.
					$repopulation = array(	'username'	=> set_value('username'),
											'email1'	=> set_value('email1'),
											'email2'	=> set_value('email2'),
											'birthday'	=> set_value('birthday'),
											'gender'	=> array(	'male'		=> FALSE,
																	'female'	=> FALSE,
																	'unknown'	=> FALSE
																	)
											);
					
					//Finding out what value the gender should have (default is unknown).
					switch (set_value('gender')) {
						case 'male':
							$repopulation['gender']['male'] = TRUE;
							break;
						case 'female':
							$repopulation['gender']['female'] = TRUE;
							break;
						default:
							$repopulation['gender']['unknown'] = TRUE;
					}
					
					//Putting the entered userdata into the page variable.
					$page_data['repopulation'] = $repopulation;
				//End repopulating the form
				
				//Loading the attributs for the popup window for the terms of use.
				$page_data['popup_attributes'] = array(	'width'			=> '800',
														'height'		=> '600',
														'scrollbars'	=> 'yes',
														'status'		=> 'yes',
														'resizable'		=> 'yes',
														'screenx'		=> '0',
														'screeny'		=> '0'
														);
				
				//Load page for signup form.
				$this->load->view('pages/v_signup', $page_data);
			}
			else {
				//Loading parameters needed to create the user.
				$parameters['username']	= $this->input->post('username');
				$parameters['password']	= $this->input->post('password1');
				$parameters['birthday']	= $this->input->post('birthday');
				$parameters['email']	= $this->input->post('email1');
				
				//Converting gender name to tinyint(2).
				switch ($this->input->post('gender')) {
					case 'male':
						$parameters['gender'] = 1;
						break;
					case 'female':
						$parameters['gender'] = 2;
						break;
					default:
						$parameters['gender'] = 0;
				}
				
				//Creating an user object.
				$this->load->model('m_user', 'user');
				
				//Creating the user.
				$success = $this->user->create($parameters);
				
				//Determine what message to show based on the result from the user creation.
				if ($success) {
					$this->load->view('pages/v_signup_success');
				}
				else {
					$this->load->view('pages/v_signup_fail');
				}
			}
		}
		
		//Loading page footer.
		$this->load->view('templates/v_footer');
	}
	
	public function terms_of_use() {
		//Loading the terms of use view.
		$this->load->view('pages/v_terms_of_use');
	}
	
	public function _birtday_check($birtday) {
		//Setting a error message for the callback.
		$this->form_validation->set_message('_birtday_check', 'The %s is not a valid date.');
		
		//Removing the dashes (-) so both yyyy-mm-dd and yyyymmdd are allowed.
		$birtday = str_replace('-', '', $birtday);
		
		//Extracting the parts of the date (that now should be in the format yyyymmdd).
		$year	= substr($birtday, 0, 4);
		$month	= substr($birtday, 4, 2);
		$day	= substr($birtday, 6, 2);
		
		//die("Year:$year; Month:$month; Day:$day; checkdate:" . checkdate($month, $day, $year) . ";"); //DEBUG
		
		//Controlling that the parts of the date is numeric.
		if (!is_numeric($year) || !is_numeric($month) || !is_numeric($day)) {
			return FALSE;
		}
		
		//Controlling that the birthday isn't set to a date in the future or to far back in time. Also checking that the date is valid.
		if ($year > date('Y') || $year < (date('Y') - 150) || !checkdate($month, $day, $year)) {
			return false;
		}
		
		//If we have got here the birtday is valid.
		return TRUE;
	}
	
	public function _gender_check($gender) {
		//Checking that a valid gender was selected (a bad user might have changed the HTML code or the post-data).
		if ($gender == 'male' || $gender == 'female' || $gender == 'unknown') {
			return TRUE;
		}
		
		//Setting a error message for the callback.
		$this->form_validation->set_message('_gender_check', 'The %s selected is not a valid gender.');
		
		return FALSE;
	}
	
	public function _agreement_check($agreement) {
		//Checking if the user agreeded to the terms of use.
		if ($agreement != FALSE) {
			return TRUE;
		}
		
		//Setting a error message for the callback.
		$this->form_validation->set_message('_agreement_check', 'You have to agree to the %s to use this web site.');
		
		return FALSE;
	}
}
?>